package com.tdj.cdtv.common;

import org.apache.commons.lang3.time.DateFormatUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @className:${name}
 * @author: Administrator
 * @date: 2018/7/5
 * @version:1.0.0
 */
public class DateUtil extends org.apache.commons.lang3.time.DateUtils{

    public static String[] parsePatterns = { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm","HH:mm" };

    public  static String getDate(){

        return getDate(parsePatterns[0]);

    }

    /**
     *  通过表达式获取到不同的日期
     * @param pattern
     * @return
     */
    public static String getDate(String pattern) {
        return DateFormatUtils.format(new Date(), pattern);
    }

    /**
     * 多少秒之后
     * @param miao 秒
     */
    public static Date getFaterTime(int miao) {
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.SECOND,miao);
        return calendar.getTime();
    }





    /**
     * 是否是日期
     * @param timeString
     * @return
     */
    public static boolean isDate(String timeString){
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        format.setLenient(false);
        try{
            format.parse(timeString);
        }catch(Exception e){
            return false;
        }
        return true;
    }

    /**
     * 字符串转date
     * @param dateString
     * @return
     */
    public static  Date stringToDate(String dateString ){

        try {
            return parseDate(dateString.toString(), parsePatterns);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * data转字符串
     * @param date
     * @param sdf
     * @return
     */
    public  static  String dateToString(Date date, String  sdf){
        if(sdf.isEmpty()){
            sdf=parsePatterns[0];
        }
        return	 DateFormatUtils.format(date,sdf);
    }
}
